package com.atguigu.tms.mock.service.impl;

import com.atguigu.tms.mock.bean.BaseComplex;
import com.atguigu.tms.mock.bean.BaseOrgan;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.mapper.BaseOrganMapper;
import com.atguigu.tms.mock.service.BaseComplexService;
import com.atguigu.tms.mock.service.BaseOrganService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 机构范围表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-13
 */
@Service
public class BaseOrganServiceImpl extends AdvServiceImpl<BaseOrganMapper, BaseOrgan> implements BaseOrganService {


    @Autowired
    BaseComplexService baseComplexService;


    Map<Long,BaseOrgan> regionOrganMap = new HashMap<>();

    public  List<BaseOrgan> getBaseOrganL1List(){
        List<BaseOrgan> baseOrganL1List = this.list(new QueryWrapper<BaseOrgan>().eq("org_level", 1));
        return  baseOrganL1List;
    }

    public void  initBaseOrgan(String cityIds){

        List<BaseOrgan> baseOrgan1List = this.baseMapper.selectBaseOrganCityList(cityIds);
        saveOrUpdateBatch(baseOrgan1List,1000,true);

        for (BaseOrgan baseOrgan : baseOrgan1List) {
            List<BaseOrgan> baseOrgan2List = this.baseMapper.selectBaseOrganDistList( baseOrgan );
            saveOrUpdateBatch(baseOrgan2List,1000,true);
        }
        loadCache();


    }

    @Override
    protected   void loadCache(List<BaseOrgan> list){
        super.loadCache(list);

        for (BaseOrgan baseOrgan : list) {
            regionOrganMap.put(baseOrgan.getRegionId(),baseOrgan);
        }

    }



    public BaseOrgan getOrganByRegionId(Long regionId){
        BaseOrgan baseOrgan = regionOrganMap.get(regionId);
        return baseOrgan;
    }

}
